﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using LeadVinaglue.Utility;
using LeadVinaglue.Models.ViewPages;
using LeadVinaglue.Models;


namespace LeadVinaglue.Areas.MyAccount.Controllers
{
    public class UserSettingsController : Controller
    {
        EFRoleProvider provider = new EFRoleProvider();
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        // Create an instance of user manage
        LeadVinaglue.Repository.UserManagementRepository repository = new LeadVinaglue.Repository.UserManagementRepository();

        // User settings
        #region #User settings
        [Authorize(Roles = "ClientAdmin")]
        public ActionResult Index()
        {
            UserManagementModel model = new UserManagementModel();

            // Get current user id
            var user = new LeadVinaglue.Repository.UserRepository().GetSingle(User.Identity.Name);
            model = repository.GetModel("Menu", "ClientAdmin", user.ID);

            return View(model);
        }

        [HttpPost]
        [Authorize(Roles = "ClientAdmin")]
        public ActionResult Index(FormCollection form)
        {
            LeadVinaglue.Repository.UserRepository repository = new LeadVinaglue.Repository.UserRepository();

            // Get current user id
            var user = repository.GetSingle(User.Identity.Name);
            user.Fullname = form["Fullname"].ToString();
            user.FirstName = form["FirstName"].ToString();
            user.LastName = form["LastName"].ToString();
            if (!string.IsNullOrEmpty(form["Password"].ToString()))
                user.Password = EncryptionData.Encryption(form["Password"].ToString());

            repository.Save(user);

            return RedirectToAction("Index");
        }

        #endregion

        
    }
}
